<template>
  <div>
    <div style="display: flex;">
      <el-button type="primary" style="height: 80%" @click="export2Excel()">导出</el-button>
      <el-tabs
        @tab-click="aval={'state': Number(state)>=0?Number(state):undefined};getData()"
        v-model="state"
      >
        <el-tab-pane label="全部订单" name="-1"></el-tab-pane>
        <el-tab-pane
          v-for="i in states"
          :key="i.value"
          :label="i.label"
          :name="i.value.toString()"
        />
      </el-tabs>
      <el-tag
        style="margin-left: 10px;"
        type="info"
      >当前页订单金额:{{pageMoney.sum.CNY}}元 {{pageMoney.sum.point}}积分</el-tag>
      <el-tag
        style="margin-left: 10px;margin-right: 10px;"
        type="info"
      >实际支付:{{pageMoney.payment.CNY}}元 {{pageMoney.payment.point}}积分</el-tag>
      <el-button type="primary" style="height: 80%" @click="optdisplay=true">加入条件</el-button>
    </div>
    <el-table :data="tableData" border style="width: 100%">
      <el-table-column prop="ID" label="ID" width="140"></el-table-column>
      <el-table-column prop="name" label="商品名称" />
      <el-table-column prop="dsum" label="订单数量" width="160" />
    </el-table>
    <el-dialog title="条件选择" :visible.sync="optdisplay" width="500">
      <!-- @tab-click="" -->

      <el-tabs v-model="optval.opt">
        <el-tab-pane
          v-for="i in optmenu"
          :key="i.value"
          :label="i.label"
          :name="i.value.toString()"
        />
      </el-tabs>
      <el-date-picker
        v-model="optval.date"
        type="datetimerange"
        align="right"
        unlink-panels
        range-separator="至"
        start-placeholder="开始日期"
        end-placeholder="结束日期"
        :default-time="['00:00:00', '23:59:59']"
        :picker-options="pickerOptions"
      />
      <el-select v-model="optval.address" placeholder="请选择">
        <el-option label="取消选择" :value="undefined" />
        <el-option
          v-for="item in addrs"
          :key="item.ID"
          :label="item.name"
          :value="item.name"
          :disabled="!item.state"
        />
      </el-select>
      <span slot="footer" class="dialog-footer">
        <el-button @click="optdisplay = false">取 消</el-button>
        <el-button type="primary" @click="optdisplay = false;mopt(true)">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
// import { export_json_to_excel } from "../vendor/Export2Excel.js";
export default {
  data() {
    return {
      states: [
        { value: 0, label: "已禁用" },
        { value: 1, label: "待支付" },
        { value: 2, label: "待配送" },
        { value: 3, label: "待收货" },
        { value: 4, label: "已完成" }
      ],
      tableData: [],
      state: "-1",
      aval: {},
      pageMoney: {
        sum: { CNY: 0, point: 0 },
        payment: { CNY: 0, point: 0 }
      },
      optdisplay: false,
      addrs: [],
      optval: {
        opt: "createTime"
      },
      optmenu: [
        { value: "createTime", label: "下单时间" },
        { value: "useTime", label: "支付时间" }
      ],
      pickerOptions: {
        shortcuts: [
          {
            text: "今天",
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(new Date(start.toDateString()).getTime());
              end.setTime(start.getTime() + (3600 * 24 - 1) * 1000);
              picker.$emit("pick", [start, end]);
            }
          },
          {
            text: "昨天",
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(
                new Date(start.toDateString()).getTime() - 3600 * 1000 * 24
              );
              end.setTime(start.getTime() + (3600 * 24 - 1) * 1000);
              picker.$emit("pick", [start, end]);
            }
          },
          {
            text: "最近一周",
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 7);
              picker.$emit("pick", [start, end]);
            }
          },
          {
            text: "最近一个月",
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 30);
              picker.$emit("pick", [start, end]);
            }
          },
          {
            text: "最近三个月",
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 90);
              picker.$emit("pick", [start, end]);
            }
          }
        ]
      },
      exportData: []
    };
  },
  methods: {
    export2Excel() {
      require.ensure([], () => {
        const { export_json_to_excel } = require("../vendor/Export2Excel.js");
        const tHeader = [
          "ID",
          "商品ID",
          "商品名称",
          "数量",
          "支付金额",
          "支付积分",
          "订单金额",
          "订单积分",
          "收货人",
          "收货电话",
          "收货地址",
          "支付订单"
        ]; //生成Excel表格的头部标题栏
        const filterVal = [
          "ID",
          "itemID",
          "itemName",
          "sum",
          "payment_CNY",
          "payment_point",
          "sum_CNY",
          "sum_point",
          "name",
          "tel",
          "address",
          "tradeNo"
        ]; //生成Excel表格的内容栏（根据自己的数据内容属性填写）
        const list = this.exportData; //需要导出Excel的数据
        // console.log(this.$cp(list));
        // return;
        const data = this.formatJson(filterVal, list);
        export_json_to_excel(tHeader, data, "人事表"); //这里可以定义你的Excel表的默认名称
      });
    },
    formatJson(filterVal, jsonData) {
      return jsonData.map(v => filterVal.map(j => v[j]));
    },
    mopt(get) {
      let date = false;
      if (this.optval.date) {
        date = this.optval.date;
      }
      // else {
      //   const end = new Date();
      //   const start = new Date();
      //   start.setTime(new Date(start.toDateString()).getTime());
      //   end.setTime(start.getTime() + (3600 * 24 - 1) * 1000);
      //   date = [start, end];
      // }
      if (date) {
        this.aval.other =
          " and " +
          this.optval.opt +
          ">" +
          parseInt(date[0] / 1000) +
          " and " +
          this.optval.opt +
          "<" +
          parseInt(date[1] / 1000);
      } else this.aval.other = undefined;
      if (this.optval.address) {
        this.aval.address = "like " + this.optval.address + "%";
      } else this.aval.address = undefined;
      if (get) this.getData();
      // console.log(this.$cp(this.optval));
      // console.log(this.$cp(this.aval));
    },
    // 时间戳转换成时间
    formatTime(time) {
      var date = new Date(time * 1000); //时间戳为10位需*1000，时间戳为13位的话不需乘1000
      return date.toLocaleString();
    },
    // 获取数据
    getData() {
      this.mopt();
      this.pageMoney = {
        sum: { CNY: 0, point: 0 },
        payment: { CNY: 0, point: 0 }
      };
      let d = {
        cmd: 0,
        val: {
          token: sessionStorage.token,
          table: "orders"
          // show: "itemID,sum,money,itemInfo"
        }
      };
      d.val.other = undefined;
      if (this.aval) d.val = Object.assign(d.val, this.aval);
      this.$axios.post("q.php", d).then(res => {
        // console.log(this.$cp(d));
        // console.log(this.$cp(res.data));
        const ret = [];
        let i = 0;
        if (res.data.result) {
          const data = res.data.result;
          const tmp = [];
          // console.log(this.$cp(data[0]));
          for (let k = 0; k < data.length; k++) {
            this.pageMoney.sum.CNY += parseInt(data[k].money.sum.CNY);
            this.pageMoney.sum.point += parseInt(data[k].money.sum.point);
            this.pageMoney.payment.CNY += parseInt(data[k].money.payment.CNY);
            this.pageMoney.payment.point += parseInt(
              data[k].money.payment.point
            );
            i = ret.id2xb(data[k].itemID);
            if (i >= 0) {
              ret[i].dsum += data[k].sum;
            } else {
              ret.push({
                ID: data[k].itemID,
                dsum: data[k].sum,
                name: data[k].itemInfo.name
              });
            }
            this.exportData.push({
              ID: data[k].ID,
              itemID: data[k].itemID,
              itemName: data[k].itemInfo.name,
              sum: data[k].sum,
              payment_CNY: data[k].money.payment.CNY / 100,
              payment_point: data[k].money.payment.point,
              sum_CNY: data[k].money.payment.CNY / 100,
              sum_point: data[k].money.payment.point,
              name: data[k].name,
              tel: data[k].tel,
              address: data[k].address,
              tradeNo: data[k].tradeNo
            });
          }
          this.pageMoney.sum.CNY /= 100;
          this.pageMoney.payment.CNY /= 100;
          this.tableData = ret;
        }
      });
    },
    upData(cmd) {
      if (!cmd || !cmd.ID) return;
      const data = {
        cmd: 1,
        val: {
          token: sessionStorage.token,
          table: "orders"
        }
      };
      data.val = Object.assign(data.val, cmd);
      this.$axios.post("q.php", data).then(res => {
        if (res.data.result) {
          this.$message({
            showClose: true,
            message: "修改数据成功",
            type: "success"
          });
        } else {
          this.$message({
            showClose: true,
            message: "修改数据失败!服务器繁忙或登陆过期",
            type: "error"
          });
        }
      });
    }
  },
  created() {
    this.$axios
      .post("q.php", {
        cmd: 0,
        val: { token: sessionStorage.token, table: "address" }
      })
      .then(res => {
        this.addrs = res.data.result;
      });
    this.getData();
  }
};
</script>
<style scoped>
.aaa {
  overflow: hidden;
  word-break: keep-all !important;
}
</style>